package com.example.carrierapp.ui.dialog;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.carrierapp.util.DisplayUtil;
import com.example.carrierapp.util.LogUtil;
import com.example.lb.fingerdemo.R;
import com.suke.widget.SwitchButton;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DispatchModifyDialog extends Dialog {

    @BindView(R.id.et_dispatch_search)
    TextView mEtName;
    @BindView(R.id.et_dispatch_carnumber)
    TextView mEtCarnumber;
    @BindView(R.id.et_dispatch_volume)
    EditText mEtVolume;

    @BindView(R.id.tv_add_times)
    TextView mTvTimes;
    @BindView(R.id.tv_add_deadline)
    TextView mTvDeadline;
    @BindView(R.id.et_dispatch_times)
    EditText mEtTimes;

    @BindView(R.id.et_dispatch_deadline)
    TextView mEtDeadline;

    @BindView(R.id.btn_more_wifi)
    SwitchButton mSwitchButton;
    @BindView(R.id.cons_hide)
    ConstraintLayout mConsHideView;
    private int mId;
    private DatePickerDialog mDatePickerDialog;

    public DispatchModifyDialog(@Nullable Context context) {
        super(context, R.style.float_dialog);
        setContentView(R.layout.dialog_dispatch_modify);
        ButterKnife.bind(this);
        initParameter();
        initView();
        initEvent();
        setCanceledOnTouchOutside(true);

    }

    private void initEvent() {
        mSwitchButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                mConsHideView.setVisibility(View.VISIBLE);
            } else {
                mConsHideView.setVisibility(View.GONE);
            }
        });
    }

    private void initView() {

    }

    public void showDialog(String driverName, boolean isMoti, String carNum, int carId, double expectedWeight) {
        mId = carId;
        mEtVolume.setText(""+expectedWeight);
        mEtName.setText(driverName);
        mEtCarnumber.setText(carNum);
        mEtVolume.setSelection((""+expectedWeight).length());
        mEtTimes.setText("");
        mEtDeadline.setText("");

        mSwitchButton.setChecked(isMoti);
        mConsHideView.setVisibility(isMoti?View.VISIBLE:View.GONE);
        this.show();
    }

    @OnClick({R.id.ib_modify_close})
    public void onCloseClick(View v) {
        dismiss();
    }

    @OnClick({R.id.et_dispatch_carnumber})
    public void onCarNumClick(View v) {
        if (mOnVolumeClickListeber != null) {
            mOnVolumeClickListeber.onModifyCarNumClick();
        }
    }

    @OnClick(R.id.tv_modify_sure)
    public void onSureClick() {
        if (mOnVolumeClickListeber != null) {
            mOnVolumeClickListeber.onModifyClick(mEtCarnumber.getText().toString().trim(),mId, mEtVolume.getText().toString().trim(), mSwitchButton.isChecked(), mEtTimes.getText().toString(), mEtDeadline.getText().toString());
        }
    }

    private void initParameter() {
        Window window = getWindow(); // 得到对话框
        WindowManager.LayoutParams wl = window.getAttributes();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
        wl.width = d.getWidth() - DisplayUtil.dip2px(getContext(), 50);
        wl.gravity = Gravity.CENTER;
        window.setAttributes(wl);
    }
    @OnClick(R.id.et_dispatch_deadline)
    public void showTimePick() {
        if (mDatePickerDialog == null) {
            Calendar calendar = Calendar.getInstance();
            //获取年月日时分的信息
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH) + 1;
            int day = calendar.get(Calendar.DATE);
            mDatePickerDialog = new DatePickerDialog(getContext(), (view, year1, monthOfYear, dayOfMonth) -> {
                LogUtil.d("showTimePick-" + year1 + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
                String date = year1 + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
                mEtDeadline.setText(date);
            }, year, month - 1, day);
        }
        if (!mDatePickerDialog.isShowing()) {
            mDatePickerDialog.show();
        }
    }


    public void setCarNum(int id, String carNum) {
        mId = id;
        mEtCarnumber.setText(carNum);
    }

    public interface OnVolumeClickListeber {
        void onModifyClick(String carNum,int carId, String volume, boolean isMo, String times, String deadLine);

        void onModifyCarNumClick();
    }

    private OnVolumeClickListeber mOnVolumeClickListeber;

    public void setOnVolumeClickListeber(OnVolumeClickListeber listeber) {
        mOnVolumeClickListeber = listeber;
    }
}
